<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="手机号" prop="anchorPhone">
        <el-input v-model="queryParams.anchorPhone" placeholder="请输入手机号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="性别" prop="anchorSex">
        <el-select v-model="queryParams.anchorSex" placeholder="请选择性别" clearable>
          <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="开播次数" prop="boNum">
        <el-input v-model="queryParams.boNum" placeholder="请输入开播次数" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="认证时间">
        <el-date-picker
          v-model="daterangeRzDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['user:anchor:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['user:anchor:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['user:anchor:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['user:anchor:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="anchorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主播id" align="center" prop="id"/>
      <el-table-column label="UserID" align="center" prop="userId"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="头像" align="center" prop="anchorImage" width="100">
        <template slot-scope="scope">
          <img :src="scope.row.anchorImage" alt="" style="width: 100px ; height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="anchorPhone"/>
      <el-table-column label="性别" align="center" prop="anchorSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.anchorSex"/>
        </template>
      </el-table-column>
      <el-table-column label="账户余额" align="center" prop="anchorMoney"/>
      <el-table-column label="开播次数" align="center" prop="boNum"/>
      <el-table-column label="在线状态" align="center" prop="zxStatus"/>
      <el-table-column label="主播认证" align="center">
        <template slot-scope="scope">
          {{ scope.row.rzStatus == 1 ? '已认证' : '' }}
          {{ scope.row.rzStatus == 2 ? '未认证' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="认证时间" align="center" prop="rzDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rzDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['user:anchor:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['user:anchor:remove']">删除
          </el-button>
          <el-button size="mini" type="text" v-if="scope.row.rzStatus==2" @click="handleRZ(scope.row)">认证</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleZB(scope.row)">详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--    //认证对话框-->
    <el-dialog title="认证信息" :visible.sync="rzOpen" width="30%" append-to-body>
      <template>
        <el-tabs v-model="activeName"  type="card" @tab-click="handleClick">
          <el-tab-pane label="直播记录" name="first">
            <!--    直播记录对话框-->
              <el-table v-loading="loading" :data="liveList" @selection-change="handleSelectionChange">
                <el-table-column label="序号" align="center" prop="id"/>
                <el-table-column label="直播标题" align="center" prop="zbName"/>
                <el-table-column label="类型" align="center" prop="zbType"/>
                <el-table-column label="时长" align="center" prop="ZbDuration"/>
                <el-table-column label="状态" align="center" prop="zbStatus"/>
                <el-table-column label="人数" align="center" prop="zbPeople"/>
                <el-table-column label="打赏金额" align="center" prop="zbMoney"/>
                <el-table-column label="添加时间" align="center" prop="addDate"/>
              </el-table>
          </el-tab-pane>
          <el-tab-pane label="认证信息" name="second">
            <el-form ref="form" :model="pojo" :rules="rules" label-width="80px">
              <el-form-item label="用户名">
                <el-input v-model="pojo.userName" placeholder="请输入真实姓名"/>
              </el-form-item>
              <el-form-item label="手机号">
                <el-input v-model="pojo.anchorPhone" placeholder="请输入手机号"/>
              </el-form-item>
              <el-form-item label="认证意见" prop="rzStatus">
                <el-radio v-model="pojo.rzStatus" :label=1>通过</el-radio>
                <el-radio v-model="pojo.rzStatus" :label=2>不通过</el-radio>
              </el-form-item>
              <el-form-item label="备注" prop="rzRemark">
                <el-input v-model="pojo.rzRemark" placeholder="请输入备注"/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleRZSubmit">提交</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="角色管理" name="third">角色管理</el-tab-pane>
          <el-tab-pane label="定时任务补偿" name="fourth">定时任务补偿</el-tab-pane>
        </el-tabs>
      </template>

    </el-dialog>
    <!-- 添加或修改主播管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="UserID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入UserID"/>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="头像" prop="anchorImage">
          <image-upload v-model="form.anchorImage"/>
        </el-form-item>
        <el-form-item label="手机号" prop="anchorPhone">
          <el-input v-model="form.anchorPhone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="性别" prop="anchorSex">
          <el-select v-model="form.anchorSex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账户余额" prop="anchorMoney">
          <el-input v-model="form.anchorMoney" placeholder="请输入账户余额"/>
        </el-form-item>
        <el-form-item label="开播次数" prop="boNum">
          <el-input v-model="form.boNum" placeholder="请输入开播次数"/>
        </el-form-item>
        <el-form-item label="认证时间" prop="rzDate">
          <el-date-picker clearable
                          v-model="form.rzDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择认证时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAnchor, getAnchor, delAnchor, addAnchor, updateAnchor, getRz, getZB} from "@/api/user/anchor";


export default {
  name: "Anchor",
  dicts: ['sys_user_sex'],
  data() {
    return {
      liveList:[],
      rzOpen: false,
      pojo: {
        rzRemark: null,
        rzStatus: null,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主播管理表格数据
      anchorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 认证时间时间范围
      daterangeRzDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        anchorPhone: null,
        anchorSex: null,
        boNum: null,
        rzDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      activeName: 'first'
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //查看直播记录
    handleZB(row){
      this.rzOpen = true;
      getZB(row.id).then(response => {
        this.liveList = response.data;
        this.$modal.msgSuccess("查询直播成功");
      })
    },
    // 认证
    handleRZ(row) {
      this.reset();//重置
      const id = row.id // 获取当前行的id
      getAnchor(id).then(response => {
        this.pojo.id = id;
        this.pojo = response.data;
        this.pojo.rzStatus = null;
        this.rzOpen = true;
        this.title = "认证信息";
      })
    },
    handleRZSubmit(){//认证通过按钮
      getRz(this.pojo.id).then(response => {
        this.$modal.msgSuccess("认证成功");
        this.rzOpen = false;
        this.getList();
      });
    },
    /** 查询主播管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRzDate && '' != this.daterangeRzDate) {
        this.queryParams.params["beginRzDate"] = this.daterangeRzDate[0];
        this.queryParams.params["endRzDate"] = this.daterangeRzDate[1];
      }
      listAnchor(this.queryParams).then(response => {
        this.anchorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        anchorImage: null,
        anchorPhone: null,
        anchorSex: null,
        anchorMoney: null,
        boNum: null,
        zxStatus: null,
        rzStatus: null,
        rzDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRzDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加主播管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAnchor(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改主播管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAnchor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAnchor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除主播管理编号为"' + ids + '"的数据项？').then(function () {
        return delAnchor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/anchor/export', {
        ...this.queryParams
      }, `anchor_${new Date().getTime()}.xlsx`)
    },

    //头像上传
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },

    handlePictureCardPreview(file) {// 图片
      this.dialogImageUrl = file.anchorImage;
      this.dialogVisible = true;
    },
    handleUploadSuccess(response) {// 上传成功
      // 在这里可以从 response 中获取上传成功后的 URL
      this.form.anchorImage = response.data;
      console.log('上传成功', response);
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
  }
};
</script>
